<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/22
 * Time: 10:42
 */

namespace app\api\model;

class Goods extends Banner
{
    /**
     * 多条查询
     * @param $where     查询条件
     * @param $field     查询字段
     * @param $page      页码
     * @param $type      使用场景  0.符合条件的所有商品  1.首页推荐产品   2.商品列表   3.商品详情随机8个  4.H5取两条
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function selectData($where = [], $field = '*', $page = 1, $type = 1){
        $query = self::where($where)
            ->where('goods_status', 1)
            ->field($field);
        if($type == 1){
            $query->order('goods_time desc')->limit(9);
        }elseif($type == 2){
            $query->order('goods_time desc')->page($page, self::$limit);
        }elseif ($type == 3){
            $query->orderRaw('rand()')->limit(8);
        }elseif ($type == 4){
            $query->order('goods_time desc')->limit(2);
        }
        $data = $query->select();
        return $data;
    }

    /**
     * 单条查询
     * @param $where
     * @param $field
     * @return array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function findData($where = [], $field = '*'){
        return self::where($where)->field($field)->find();
    }

    /**
     * 统计数量
     * @param $where
     * @return int|string
     * @throws \think\Exception
     */
    static function countData($where = []){
        return self::where($where)->count();
    }

    /**
     * 读取器，获取完整图片绝对路径
     * @param $url  图片地址
     * @param $data 表中所有数据组成的数组，框架根据当前模型自动获取
     * @return string
     */
    public function getGoodsThumbAttr($url, $data)
    {
        if($data['goods_thumb']){
            return $this->prefixImgUrl($url);
        }else{
            return '';
        }

    }
}